Welcome Guest | Sign in | Register

Home > Java Programming > Inner Classes > Questions and Answers

01. What is the output for the below code ?
public class Outer {
private int a = 7;
class Inner {
public void displayValue() {
System.out.println("Value of a is " + a);
}
}
}
public class Test {
public static void main(String... args) throws Exception {
Outer mo = new Outer();
Outer.Inner inner = mo.new Inner();
inner.displayValue();
}
}

A. Value of a is 7 B. Compilation Fails
C. Compilation succeed but Runtime Exception D. Value of a is 0

Answer and Explanation

Answer: Value of a is 7

Explanation:
an inner class instance can never stand alone without a direct relationship to an
instance of the outer class. you can access the inner class is through a live instance of
the outer class. Inner class can access private member of the outer class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. Modifiers Applied to Inner Classes is ________________?
A. private B. final
C. public D. static
E. All of above

Answer and Explanation

Answer: All of above

Explanation:
A, B, C and D is the correct answer.
Modifiers Applied to Inner Classes is final,abstract,public,private,protected,static

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What is the output for the below code ?
class Outer {
private String x = "Outer variable";
void doStuff() {
String z = "local variable";
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
}
}
}
}
A. Compile Fail : local variable z is can't access from within inner class B. Compilation succeed but Runtime Exception
C. Compile without error. D. None of the above

Answer and Explanation

Answer: Compile Fail : local variable z is can't access from within inner class

Explanation:
the inner class object cannot use the local variables of the method the inner class is in.only final can be accessible.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What is the output for the below code ?
public class Tech {
public void tech() {
System.out.println("Tech");
}
}
public class Atech {
Tech a = new Tech() {
public void tech() {
System.out.println("anonymous tech");
}
};
public void dothis() {
a.tech();
}
public static void main(String... args){
Atech atech = new Atech();
atech.dothis();
}
}
A. anonymous tech B. Tech
C. Compilation fails D. None of the above

Answer and Explanation

Answer: anonymous tech

Explanation:
This is anonymous subclass of the specified class type. anonymous inner class
( anonymous subclass ) overriden the Tech super class of tech() method. So Subclass method will get called.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the output for the below code ?
public interface Ianimal {
public void eat();
}
public class Tiger {
Ianimal c = new Ianimal() {
public void eat() {
System.out.println("Tiger eat non-veg");
}
};
public void doit(){
c.eat();
}
public static void main(String... args){
Tiger t = new Tiger();
t.doit();
}
}
A. Tiger eat non-veg B. Compilation succeed but Runtime Exception
C. Compile fail : Interface can't Instantiate D. None of the above

Answer and Explanation

Answer: Tiger eat non-veg

Explanation:
This is anonymous implementer of the specified interface type. Interface is
implmented by anonymous class and output is "Tiger eat non-veg".

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Is the bellow code compile without error?
public class ThreadTest {
Runnable r = new Runnable() {
public void run() { }
};
}
A. compile without error B. compile with error because Runnable is an Interface.
C. Compilation succeed but Runtime Exception D. None of the above

Answer and Explanation

Answer: compile without error

Explanation:
instantiating an implementer of the Runnable interface (an anonymous
implementation class). So compile without error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What is the true about the bellow code
class Outer {
static class Inner { }
}
A. Inner class can be accessed without having an instance of the outer class. B. .Inner class can't be accessed without having an instance of the outer class.
C. Compile error D. None of the above

Answer and Explanation

Answer: Inner class can be accessed without having an instance of the outer class.

Explanation:
Static Inner class can be accessed without having an instance of the outer class. This is like other static members.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. What is the output for the below code ?
class Outer {
static class Inner {
void go() {
System.out.println("Inner.go");
}
}
}
public class Test {
static class Inner1 {
void go() {
System.out.println("Inner1.go");
}
}
public static void main(String... args) throws Exception {
Outer.Inner bn = new Outer.Inner();
bn.go();
Inner1 in = new Inner1();
in.go();
}
}
A. Comile fail because Inner1 in = new Inner1(); Inner class can't be accessed without
having an instance of the outer class
B. Inner.go Inner1.go
C. Runtime Exception D. Runtime Exception

Answer and Explanation

Answer: Inner.go Inner1.go

Explanation:
Static Inner class can be accessed without having an instance of the outer class. This is like other static members.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Which are true about an anonymous inner class?
A. can extend exactly one class and implement exactly one interface B. can extend exactly one class or implement exactly one interface.
C. can implement multiple interfaces if it does not extend a class. D. can implement multiple interfaces if it does not extend a class.

Answer and Explanation

Answer: can extend exactly one class or implement exactly one interface.

Explanation:
syntax of an anonymous inner class allows for only one. can extend exactly one class or implement exactly one interface. Ianimal c = new Ianimal() { } Ianimal can be
class or interface.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. What will be the result of compiling and run the following code:
public class Test {
public static void main(String... args) throws Exception {
Object o = new Object() {
public boolean equals(Object obj) {
return true;
}
}
System.out.println(o.equals("test"));
}
}
A. Compile Fail: semicolon missing at Object o = new Object() { public boolean
equals(Object obj) { return true; } };
B. test
C. Compilation succeed but Runtime Exception D. None of the above

Answer and Explanation

Answer: Compile Fail: semicolon missing at Object o = new Object() { public boolean
equals(Object obj) { return true; } };

Explanation:
semicolon missing at Object o = new Object() { public boolean equals(Object obj)
{ return true; } };

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.